﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using King.Engine.GameObjects;
using Microsoft.Xna.Framework;
using King.Engine.Components;

namespace BreacherCreature.GameObjects.Hud
{
    class HUDMessageBox : GameObject
    {
        #region fields
        private int messageTimeoutInterval = 10;
        private GameTime gameTime;
        #endregion

        #region constants
        /// <summary>
        /// Speed of the moving text in the message box. pixels per second.
        /// </summary>
        int SPEED = 50;
        int MESSAGE_LINE_HEIGHT = 13;
        #endregion

        #region constructor

        public HUDMessageBox(Vector2 location, int messageTimeoutInterval): base(true,true,true)
        {
            this.Location = location;
            this.messageTimeoutInterval = messageTimeoutInterval;
        }
        
        #endregion

        #region overrides
        protected override void UpdateBeforeComponents(GameTime gameTime)
        {
            this.gameTime = gameTime;
            base.UpdateBeforeComponents(gameTime);
            List<Label> messageList = this.GetComponents<Label>().ToList<Label>();
            int count = 0;

            foreach(Label message in messageList)
            {
                int x = (DateTime.UtcNow - message.initTime).Seconds;
                if (x > this.messageTimeoutInterval)
                {
                    this.RemoveComponent(messageList.First<Label>());
                }
                else {
                    if (message.Location.Y > 0 + count * this.MESSAGE_LINE_HEIGHT)
                    {
                        message.Location = message.Location - new Vector2(0, (float)(SPEED*gameTime.ElapsedGameTime.TotalSeconds) );
                    }
                    count++;
                }
            }
        }
        #endregion

        #region internal

        /// <summary>
        /// returns a label with preset settings.
        /// </summary>
        /// <param name="text">Message</param>
        /// <param name="color">color of the message</param>
        /// <returns>Label</returns>
        internal Label labelInit(String text, Color color)
        {
            return new Label("HUD_MSG_FONT",DrawDepths.HUD,true)
            {
                IsCentered=true,
                Location= new Vector2(0,60),
                Text = text,
                Color=color
            };
        }
        #endregion

        #region public methods

        /// <summary>
        /// Adds a message to the message box. Removal is automatic and happens when messageTimeoutInterval is reached.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="color"></param>
        public void addMessage(String text, Color color)
        {
            this.AddComponent(this.labelInit(text, color));
        }

        #endregion
    }
}
